package com.hfengxiang.example.canvasrotatedemo;

public class ScaleCalculator {
    public static float calculateScale(float windowWidth, float windowHeight,
                                       float viewWidth, float viewHeight,
                                       int targetDegree) {
        targetDegree = Math.abs(targetDegree % 360);
        float targetScale = 0;
        if (viewWidth > viewHeight) {
            switch (targetDegree) {
                case 0:
                case 180:
                    targetScale = windowWidth / viewWidth;
                    break;
                case 90:
                case 270:
                    if (viewHeight / viewWidth > windowWidth / windowHeight) {
                        targetScale = windowWidth / viewHeight;
                    } else {
                        targetScale = windowHeight / viewWidth;
                    }
                    break;

            }
        } else {
            switch (targetDegree) {
                case 0:
                case 180:
                    if (viewWidth / viewHeight > viewWidth / windowHeight) {
                        targetScale = windowWidth / viewWidth;
                    } else {
                        targetScale = windowHeight / viewHeight;
                    }
                    break;
                case 90:
                case 270:
                    targetScale = windowWidth / viewHeight;
                    break;
            }
        }
        return targetScale;
    }
}
